package com.example.project.controller;

import java.util.List;

import com.example.project.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.project.entity.Cart;
import com.example.project.mapper.CartMapper;

/**
 * 购物车Controller
 *
 * @author changyao
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/project/cart")
public class CartController
{
    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public List<Cart> list()
    {

        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        List<Cart> carts=cartMapper.selectList(queryWrapper);
        for (Cart cart : carts) {
            cart.setGoodsInfo(goodsMapper.selectById(cart.getCartGoods()))  ;
        }

        return carts;
    }

    @PostMapping("/ifhave")
    public List<Cart> ifhave(@RequestBody Cart cart)
    {
        QueryWrapper<Cart> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",cart.getUserId()).eq("cart_goods",cart.getCartGoods());
        return cartMapper.selectList(queryWrapper);
    }




    /**
     * 获取购物车详细信息
     */
    @PostMapping("/detil")
    public Cart getInfo(@RequestBody Cart cart)
    {
        return cartMapper.selectById(cart);
    }


    /**
     * 新增购物车
     */
    @PostMapping("/add")
    public int add(@RequestBody Cart cart)
    {
        return cartMapper.insert(cart);
    }

    /**
     * 修改购物车
     */
    @PostMapping("/edit")
    public int edit(@RequestBody Cart cart)
    {
        return cartMapper.updateById(cart);
    }

    /**
     * 删除购物车
     */
	@PostMapping("/del")
    public int remove(@RequestBody Cart cart)
    {
        return cartMapper.deleteById(cart);
    }
}
